<template>
	<div class="steps-container">
		<el-card shadow="hover" header="操作">
			<el-form :inline="true" 
					ref="ruleFormRef"
					:model="ruleForm"
					:rules="rules">
				<el-form-item prop="orgcode">
					<el-input v-model="ruleForm.orgcode"  placeholder="请输入定点编号"/>
				</el-form-item>
				<el-form-item>
					<el-button @click="readLiqData(ruleFormRef)" size="default" type="primary">
						<SvgIcon name="iconfont icon-step" />
						查询
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="print(ruleFormRef)" size="default" >
						<SvgIcon name="ele-Printer" />
						打印
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="server()" size="default"  type="warning">
						<SvgIcon name="fa fa-server" :size="30" />
						启动虚拟服务
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="write(ruleFormRef)" size="default">
						<SvgIcon name="fa fa-file" :size="30" />
						结算单数据生成
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="serverDown()" size="default"  type="warning">
						<SvgIcon name="fa fa-server" :size="30" />
						关闭虚拟服务
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover" header="报表内容">
			<div ref="printRef">
				<div id="report"></div>
			</div>
			<div id="print"></div>
		</el-card>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, defineComponent, ref } from 'vue'
import { ElMessage, FormRules, FormInstance } from 'element-plus'
import get from '/@/utils/get'
import { CHSP0001 } from '../../../Modal/CHSP0001'
import { Chs9001 } from '../../../Modal/CHS9001'
import post from '/@/utils/post'
import printJs from 'print-js'

export default defineComponent({
	name: 'randomStatement',
	setup() {
		const state = reactive({
			ruleForm: {
				tcq: true,
				orgname: '',
				orgcode: '',
				area: true
			},
			iframeDom: null as HTMLIFrameElement | null,
			printRef: null as null | HTMLDivElement,
			iframeLoading: true,
			iframeUrl: '',
			rules: reactive<FormRules>({
				orgcode: [
					{ required: true, message: '定点编号必填，不能留空', trigger: 'blur' },
				],
			})
		});
		const ruleFormRef = ref<FormInstance>()
		// 下一步点击
		const print = (formEl: FormInstance | undefined) => {
			printJs({
					printable: state.printRef,
					type: 'html',
					scanStyles: false,
					maxWidth: 1000,
					style: `@media print{.u_r06{margin-bottom:15px;}}`,
				})
				return
		}
		const readLiqData = (formEl: FormInstance | undefined) => {
			if (!formEl) return
			formEl.validate(() => {
				queryReport()
			})
		}

		const queryReport = () => {
			get.get('http://36.134.199.75:7777/neuyyfftool/api/neuyyfftool/siBalance/randomTodayRetailId').then(res=>{
				get.get('http://36.134.199.75:7777/report/Report-ResultAction.do?reportId=	REPORT-C9FC258221600001B6DE1D71660116AA'+
				'&bdflag=1'+
				'&retailid=	'+res.data.retailid.c[0]+res.data.retailid.c[1]+
				'&orgcode='+res.data.orgcode+
				'&showcode='+state.ruleForm.orgcode+
				'&loginname=drugstore'+
				'&token=Neusoft123').then(res=>{
					reportHandle(res)
				})
			})
		}

		const reportHandle = (res) => {
			let result = res.data
			while(result.indexOf('<link type="text/css"\n\thref="/report/') != -1){
				result = result.replace('<link type="text/css"\n\thref="/report/','<link type="text/css"\n\thref="../../neusoft/unieap/report/"')
			}
			result = result.replace('<img\n\t\t\tsrc="/report/unieap/pages/report/images/progressbar.gif" />','')
			result = result.replace('<img src="unieap/pages/report/jsp/show/lodop/img/loading.gif"\n\t\t\t\t\tstyle="width: 40px; height: 40px;" />','')
			document.getElementById('report').innerHTML = result
			document.getElementById('processdiv').remove()
		}

		const server = () => {
			window.open('yyfftool://jsd_print_open/?')
		}

		const serverDown = () => {
			window.open('yyfftool://jsd_print_close/?')
		}

		const write = (formEl: FormInstance | undefined) => {
			if (!formEl) return
			formEl.validate(() => {
				//从云药店后台获取必要参数
				get.get('http://36.134.199.75:7777/neuyyfftool/api/neuyyfftool/siBalance/randomTodayRetailId').then(res=>{
					//请求医保南宁正式地址-签到
					let chs = Chs9001.init()
					chs.fixmedins_code = res.data.orgcode
					chs.infno = '9001'
					chs.sign_no = ''
					chs.recer_sys_code = 'MBS_LOCAL'
					chs.opter = res.data.orgcode
					post.postList(chs, 'http://10.98.34.114/mbs-hiIntf-nanning/web/api/fsi/callService').then(res1=>{
						let chs1 = CHSP0001.init()
						chs1.fixmedins_code = res.data.orgcode
						chs1.sign_no = res1.data.output.signinoutb.sign_no
						chs1.opter = res.data.orgcode
						chs1.input.data.mdtrtId = res.data.mdtrtid
						chs1.input.data.medinsSetlId = res.data.medinssetlid
						chs1.input.data.psn_no = res.data.psnno
						chs1.input.data.setlId = res.data.setlid
						post.postList(chs1, 'http://10.98.34.114/mbs-hiIntf-nanning/web/api/fsi/callService').then(res2=>{
							if(res2.data.infcode != 0) {
								ElMessage({
									showClose: true,
									duration: 5000,
									message: '医保交易P0001失败:'+res2.data.err_msg,
									type: 'error',
								})
							} else {
								let out = res2.data
								post.postList({shopid: state.ruleForm.orgcode.slice(1)},'http://36.134.199.75:7777/neuyyfftool/api/neuyyfftool/comShop/query').then(res3=>{
									out.output = out.output.replace(/<fixmedins_name>.*<\/fixmedins_name>/g,'<fixmedins_name>'+res3.data[0].shopname+'</fixmedins_name>')
									out.output = out.output.replace(/<crter_name>.*<\/crter_name>/g,'<crter_name>'+state.ruleForm.orgcode+'</crter_name>')
									post.postList(out, 'http://localhost:9696/write').then(res=>{
										ElMessage({
											showClose: true,
											duration: 5000,
											message: ''+res.data.result,
											type: 'success',
										})
									})
								})
							}
						})
					})
				})
			})
		}
		return {
			readLiqData,
			print,
			server,
			write,
			serverDown,
			ruleFormRef,
			...toRefs(state),
		};
	}
})
</script>
